#!/usr/bin/env python

import model_data
from topics.swingset_request_handler import SwingsetRequestHandler

class OBJECT_REFERENCE(SwingsetRequestHandler):
    def index(self):
        # This is a 301 temporary redirect
        self.redirect( self.url(c="object_reference", action="insecure") )
            
    def secure(self):
        self.render_and_write("object_reference/secure.html", 
                known_usernames=model_data.get_partial_username_key_list_secure(),
                ajax_target=self.url(c="object_reference", action="get_profile_secure"),
                )
                                                
    def insecure(self):        
        self.render_and_write("object_reference/insecure.html", 
                known_usernames=model_data.get_partial_username_key_list_insecure(),
                ajax_target=self.url(c="object_reference", action="get_profile_insecure"),
                )
                
    def get_profile_insecure(self):
        key = self.request.get('key')
        user = model_data.users_map.get(key)
        
        if user is None:
            profile = "No such user!"
        else:
            profile = user.profile
            
        self.response.out.write( profile )

    def get_profile_secure(self):
        key = self.request.get('key')
        try:
            user = model_data.arm.get_direct_reference(key)
            profile = user.profile
        except Exception, e:
            profile = "%s<br>%s" % (e.get_user_message(), e.get_log_message())
            
        self.response.out.write( profile )

